package com.example.administrator.newxlmedical;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;

import com.example.administrator.baseproject.BaseActivity;

import butterknife.ButterKnife;
import constants.Constants;
import fragments.TerminalInfoFragment;
import fragments.TerminalPasswordFragment;
import netframe.NetAddress;
import utils.ActivityUtil;

public class TerminalActivity extends BaseActivity implements TerminalInfoFragment.TerminalInfoInterface, TerminalPasswordFragment.TerminalPasswordInterface {

    private TerminalInfoFragment infoFragment;
    private String hospitalName;
    private String hospitalAddress;
    private String hospitalDepartments;
    private String hospitalRemark;
    private SharedPreferences sharedPreferences;
    private String departmentCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terminal);
        ButterKnife.bind(this);
        sharedPreferences = ActivityUtil.getSharedPreferences(TerminalActivity.this);
        infoFragment = new TerminalInfoFragment();

        if (TextUtils.isEmpty(sharedPreferences.getString(Constants.MACHINE_CODE, ""))) {
            clickContent(infoFragment);
        } else {
            NetAddress.token = sharedPreferences.getString(Constants.TOKEN, "");
            NetAddress.machineCode = sharedPreferences.getString(Constants.MACHINE_CODE, "");
            NetAddress.terminalInfoId = sharedPreferences.getString(Constants.TERMINALINFO_ID, "");

            ActivityUtil.jumpToActivity(this,EnterActivity.class);
            finish();
        }

        Log.e(TAG, "token = " +sharedPreferences.getString(Constants.TOKEN, "") + "machineCode = " +sharedPreferences.getString(Constants.MACHINE_CODE, "") + "terminalInfoId" + sharedPreferences.getString(Constants.TERMINALINFO_ID, ""));
    }

    private void clickContent(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_terminal_container, fragment);
        transaction.commit();
    }

    @Override
    public void activate(String name, String address, String departments, String remark,String deptCode) {
        departmentCode = deptCode;
        hospitalName = name;
        hospitalAddress = address;
        hospitalDepartments = departments;
        hospitalRemark = remark;
        TerminalPasswordFragment passwordFragment = new TerminalPasswordFragment();
        Bundle bundle = new Bundle();
        bundle.putString("Name",hospitalName);
        bundle.putString("Departments",hospitalDepartments);
        bundle.putString("Address",hospitalAddress);
        bundle.putString("Remark",hospitalRemark);
        bundle.putString("departmentCode",deptCode);
        passwordFragment.setArguments(bundle);
        clickContent(passwordFragment);
    }

    @Override
    public void back() {
        clickContent(infoFragment);
    }

    @Override
    public void sure(String id, String tokenCode, String machineCode,String deptCode) {
        saveTerminalInfo(id, tokenCode, machineCode, deptCode);
        ActivityUtil.jumpToActivity(TerminalActivity.this,EnterActivity.class);
        finish();
    }

    //保存激活时的相关信息
    private void saveTerminalInfo(String terminalInfoId, String tokenCode, String machineCode, String departmentCode1) {

        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(Constants.TERMINALINFO_ID, terminalInfoId);
        edit.putString(Constants.TOKEN, tokenCode);
        edit.putString(Constants.MACHINE_CODE, machineCode);
        edit.putString(Constants.DEPARTMENT_CODE, departmentCode);

        edit.putString(Constants.HOSPITAL_NAME,hospitalName);
        edit.putString(Constants.HOSPITAL_ADDRESS,hospitalAddress);
        edit.putString(Constants.HOSPITAL_DEPARTMENTS,hospitalDepartments);
        edit.putString(Constants.HOSPITAL_REMARK,hospitalRemark);
        edit.commit();

        NetAddress.token = sharedPreferences.getString(Constants.TOKEN, "");
        NetAddress.machineCode = sharedPreferences.getString(Constants.MACHINE_CODE, "");
        NetAddress.terminalInfoId = sharedPreferences.getString(Constants.TERMINALINFO_ID, "");
        NetAddress.deptCode = departmentCode;
    }
}
